package drivers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import commands.UserEventGroup;
import net.base.annotations.NotNull;
import net.protocol.carfun.common.CommandResponse;
import net.socket.driver.event.EventCommandDriver;
import net.socket.exception.ClientConnectException;
import net.socket.exception.CommandExecException;
import net.socket.exception.ProtocolUnSupportException;
import net.socket.thread.SocketProxy;
import register.RountRegisterInfo;


/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/3/25 下午7:35
 * @UpdateDate:
 * @Description:
 */
public class PointEventCommandDriver implements EventCommandDriver<UserEventGroup.Points, RountRegisterInfo> {
    @Override
    public CommandResponse<JSON> dealEventCommand(@NotNull UserEventGroup.Points points,
                                                  @NotNull SocketProxy<RountRegisterInfo> socketProxy,
                                                  @NotNull RountRegisterInfo rountRegisterInfo,
                                                  @NotNull Object channelId, JSON json)
            throws ProtocolUnSupportException, CommandExecException, ClientConnectException {
        CommandResponse res = null;
        switch (points){
            case EVENT_POINT_ADD:
                res = dealEventAdd(points,socketProxy,rountRegisterInfo,channelId,json);
                break;
            case EVENT_POINT_DESC:
                res = dealEventDesc(points,socketProxy,rountRegisterInfo,channelId,json);
                break;
            case EVENT_POINT_EMPTY:
                res = dealEventEmpty(points,socketProxy,rountRegisterInfo,channelId,json);
                break;
        }
        return res;
    }

    private CommandResponse dealEventAdd(UserEventGroup.Points points,
                                         SocketProxy<RountRegisterInfo> socketProxy,
                                         RountRegisterInfo rountRegisterInfo,
                                         Object channelId, JSON json) {
        JSONObject jsonObject = new JSONObject() {{
            put("id", rountRegisterInfo.getIdentity());
            put("op",points.getCommand());
            put("reporter", json);
        }};
        return CommandResponse.success(jsonObject);
    }
    private CommandResponse dealEventDesc(UserEventGroup.Points points,
                                          SocketProxy<RountRegisterInfo> socketProxy,
                                          RountRegisterInfo rountRegisterInfo,
                                          Object channelId, JSON json) {
        JSONObject jsonObject = new JSONObject() {{
            put("id", rountRegisterInfo.getIdentity());
            put("op",points.getCommand());
            put("reporter", json);
        }};
        return CommandResponse.success(jsonObject);
    }
    private CommandResponse dealEventEmpty(UserEventGroup.Points points,
                                           SocketProxy<RountRegisterInfo> socketProxy,
                                           RountRegisterInfo rountRegisterInfo,
                                           Object channelId, JSON json) {
        JSONObject jsonObject = new JSONObject() {{
            put("id", rountRegisterInfo.getIdentity());
            put("op",points.getCommand());
            put("reporter", json);
        }};
        return CommandResponse.success(jsonObject);
    }

    @Override
    public UserEventGroup.Points[] getSupportCommands() {
        return UserEventGroup.Points.values();
    }
}
